package cn.jinjimi.app.controller;

import cn.jinjimi.app.custom.entity.ClueStatistic;
import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.custom.entity.CustomFollowsDTO;
import cn.jinjimi.app.custom.entity.CustomLastedFollow;
import cn.jinjimi.app.custom.entity.CustomListDTO;
import cn.jinjimi.app.custom.entity.CustomTagData;
import cn.jinjimi.app.custom.entity.Follow;
import cn.jinjimi.app.custom.entity.Insurance;
import cn.jinjimi.app.custom.entity.Proposal;
import cn.jinjimi.app.custom.entity.UploadCustom;
import cn.jinjimi.domain.data.net.API;
import cn.jinjimi.domain.data.net.CardService;
import cn.jinjimi.domain.data.dto.ClueDTO;
import cn.jinjimi.domain.data.dto.CustomImportDTO;
import cn.jinjimi.domain.data.dto.CustomerDto;
import cn.jinjimi.domain.data.dto.HolidayCardsDto;
import cn.jinjimi.domain.data.dto.SendCardRecordDto;
import cn.jinjimi.domain.ServiceManager;
import cn.jinjimi.domain.store.CardStore;
import cn.jinjimi.domain.store.CustomerStore;
import cn.jinjimi.app.entity.CardListBean;
import cn.jinjimi.app.entity.PhotoInfo;
import cn.jinjimi.app.entity.RecordCardBean;
import cn.jinjimi.app.entity.SceneCardRespData;
import cn.jinjimi.app.entity.SceneRespData;

import java.util.List;
import java.util.concurrent.Callable;

import bolts.Task;
import retrofit2.Response;

/**
 * Created by Rain on 2016/1/15.
 */
public class CustomerController extends BaseController {

    private static final String TAG = CustomerController.class.getSimpleName();

    /**
     * Event TodayRemindEvent
     * 获取今日提醒数据
     *
     * @return
     */
    public static Task<String> getTodayRemind() {

        return Task.callInBackground(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).getRemindOfToday();
            }
        });
    }


    /**
     * Event CardListEvent
     * 获取贺卡模板
     *
     * @return
     */
    public static Task<CardListBean> getCardList(final int type, final int page, final int limit) {

        return Task.callInBackground(new Callable<CardListBean>() {
            @Override
            public CardListBean call() throws Exception {
                return ServiceManager.getService(CardStore.class).fetchCardsWithType(type, page, limit);
            }
        });
    }


    /**
     * 获取节日贺卡（不包含生日）
     *
     * @param page
     * @param limit
     * @return
     */
    public static Task<HolidayCardsDto> getHolidayCards(final int page, final int limit) {
        return Task.callInBackground(new Callable<HolidayCardsDto>() {
            @Override
            public HolidayCardsDto call() throws Exception {
                return ServiceManager.getService(CardStore.class).fetchHolidayCards(page, limit);
            }
        });
    }

    /**
     * 获取情景贺卡列表
     *
     * @return
     */
    public static Task<SceneRespData> getSceneList() {
        return Task.callInBackground(new Callable<SceneRespData>() {
            @Override
            public SceneRespData call() throws Exception {
                return ServiceManager.getService(CardStore.class).fetchScenes();
            }
        });
    }

    /**
     * 根据场景id和page获取对应场景贺卡
     *
     * @param sceneId
     * @param page
     * @param limit
     * @return
     */
    public static Task<SceneCardRespData> getSceneCardList(final int sceneId, final int page, final int limit) {
        return Task.callInBackground(new Callable<SceneCardRespData>() {
            @Override
            public SceneCardRespData call() throws Exception {
                return ServiceManager.getService(CardStore.class).fetchSceneCards(sceneId, page, limit);
            }
        });
    }

    /**
     * 根据id获取贺卡详情
     *
     * @param cardId
     * @return
     */
    public static Task<CardListBean.CardBean> getCardById(final String cardId) {
        return Task.callInBackground(new Callable<CardListBean.CardBean>() {
            @Override
            public CardListBean.CardBean call() throws Exception {
                return ServiceManager.getService(CardStore.class).fetchCardWithId(cardId);
            }
        });
    }


    /**
     * 提交发送贺卡记录
     *
     * @param blessing
     * @param greetingCardId
     * @return
     */
    public static void uploadSendCardRecord(final String blessing, final String customerId, final String greetingCardId) {
        Task.callInBackground(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                SendCardRecordDto dto = new SendCardRecordDto(blessing, customerId, greetingCardId);
                API.create(CardService.class).uploadRecordCardInfo(dto).execute();
                return null;
            }
        });
    }


    /**
     * 跟进最近修改时间，获取所有的客户列表
     *
     * @param lastModified
     * @return
     */
    public static Task<CustomListDTO> getCustomList(final long lastModified) {

        return Task.callInBackground(new Callable<CustomListDTO>() {
            @Override
            public CustomListDTO call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).getCustomers(lastModified);
            }
        });
    }

    /**
     * 上传需要上传的客户
     * 调用改至CustomsManager处，故显示无用方法，保留谢谢 by Roy 2017/2/8
     *
     * @param contacts
     * @return
     */
    public static Task<CustomImportDTO> uploadContasts(final List<UploadCustom> contacts) throws Exception {

        return Task.callInBackground(new Callable<CustomImportDTO>() {
            @Override
            public CustomImportDTO call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).importContacts(contacts);
            }
        });
    }

    public static Task<Response<CustomerDto>> addOrUpdateCustom(Custom custom, String clueId) {
        if (custom.customerId == null || custom.customerId == 0) {
            return addCustom(custom, clueId);
        } else {
            return updateCustom(custom);
        }
    }

    /**
     * 手动新增客户
     *
     * @param custom
     * @param clueId
     * @return
     */
    private static Task<Response<CustomerDto>> addCustom(final Custom custom,
                                                         final String clueId) {
        return Task.callInBackground(new Callable<Response<CustomerDto>>() {
            @Override
            public Response<CustomerDto> call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).addCustomer(custom, clueId);
            }
        });
    }

    /**
     * 更新客户
     *
     * @param custom
     * @return
     */
    private static Task<Response<CustomerDto>> updateCustom(final Custom custom) {
        return Task.callInBackground(new Callable<Response<CustomerDto>>() {
            @Override
            public Response<CustomerDto> call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).updateCustomer(custom);
            }
        });
    }

    /**
     * 更改客户的类别
     * 准客户 / 其它客户
     * type 1 为准客户, 0 为其它的客户
     *
     * @param id
     * @param type
     */
    public static Task<CustomerDto> updateCustomType(final long id, final int type) {
        return Task.callInBackground(new Callable<CustomerDto>() {
            @Override
            public CustomerDto call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).updateCustomerType(String.valueOf(id), type);
            }
        });
    }

    /**
     * 获取指定客户的信息
     *
     * @param id
     */
    public static Task<Response<CustomerDto>> getCustomInfo(final long id) {

        return Task.callInBackground(new Callable<Response<CustomerDto>>() {
            @Override
            public Response<CustomerDto> call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).getCustomer(String.valueOf(id));
            }
        });
    }

    /**
     * 查看客户保单
     *
     * @param customerId
     * @param insuranceId
     * @return
     */
    public static Task<Insurance> getInsureInfo(final String customerId, final String insuranceId) {

        return Task.callInBackground(new Callable<Insurance>() {
            @Override
            public Insurance call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).getInsurance(customerId, insuranceId);
            }
        });
    }

    /**
     * 更新保单
     *
     * @param customerId
     * @param effect_date
     * @param insureId
     * @param name
     * @param term
     * @return
     */
    public static Task<Insurance> updateInsureInfo(final long customerId, final long effect_date, final long insureId, final String name, final int term) {
        return Task.callInBackground(new Callable<Insurance>() {
            @Override
            public Insurance call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).updateInsurance(String.valueOf(customerId),
                        effect_date, insureId, name, term);
            }
        });
    }

    /**
     * 获取保单列表
     *
     * @param customerId
     */
    public static Task<List<Insurance>> getInsureList(final long customerId) {
        return Task.callInBackground(new Callable<List<Insurance>>() {
            @Override
            public List<Insurance> call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).getInsuranceList(String.valueOf(customerId));
            }
        });
    }

    /**
     * 添加客户保单
     *
     * @param customerId
     * @param name
     * @param date
     * @param term
     * @return
     */
    public static Task<Insurance> addInsurance(final String customerId, final String name, final long date, final int term) {

        return Task.callInBackground(new Callable<Insurance>() {
            @Override
            public Insurance call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).addInsurance(customerId, name, date, term);
            }
        });
    }

    /**
     * 删除客户保单
     *
     * @param customerId
     * @param insuranceId
     * @return
     */
    public static Task<Insurance> deleteInsureInfo(final long customerId, final long insuranceId) {

        return Task.callInBackground(new Callable<Insurance>() {
            @Override
            public Insurance call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).deleteInsurance(String.valueOf(customerId), String.valueOf(insuranceId));
            }
        });
    }

    /**
     * 删除客户
     *
     * @param customer_id
     * @return
     */
    public static Task<CustomerDto> deleteCustom(final long customer_id) {

        return Task.callInBackground(new Callable<CustomerDto>() {
            @Override
            public CustomerDto call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).deleteCustomer(String.valueOf(customer_id));
            }
        });
    }

    /**
     * 上传贺卡信息
     *
     * @param blessing
     * @param customerId
     * @param customerName
     * @param cardId
     * @param holidayId
     * @param voiceDuration
     * @return
     */
    public static Task<RecordCardBean> uploadRecordCardInfo(
            final String blessing,
            final String customerId,
            final String customerName,
            final int cardId,
            final int holidayId,
            final int voiceDuration) {

        return Task.callInBackground(new Callable<RecordCardBean>() {
            @Override
            public RecordCardBean call() throws Exception {
                return ServiceManager.getService(CardStore.class).makeCard(blessing, customerId, customerName, cardId, holidayId, voiceDuration);
            }
        });

    }

    /**
     * 添加跟进记录
     *
     * @param customerId
     * @param content
     * @param followTime
     * @return
     */
    public static Task<Follow> addFollowRecord(final long customerId, final String content, final long followTime) {
        return Task.callInBackground(new Callable<Follow>() {
            @Override
            public Follow call() throws Exception {
                return ServiceManager.getService(CustomerStore.class)
                        .newFollow(customerId, content, followTime);
            }
        });
    }

    /**
     * 查看图片列表
     *
     * @param customerId
     * @return
     */
    public static Task<List<PhotoInfo>> showPhotosList(final long customerId) {

        return Task.callInBackground(new Callable<List<PhotoInfo>>() {
            @Override
            public List<PhotoInfo> call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).getCustomerPhotosList(String.valueOf(customerId));
            }
        });
    }

    /**
     * 客户跟进记录列表
     *
     * @param customerId
     * @return List&lt;Follow&gt;
     */
    public static Task<CustomFollowsDTO> getFollowList(final long customerId) {
        return Task.callInBackground(new Callable<CustomFollowsDTO>() {
            @Override
            public CustomFollowsDTO call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).getFollows(String.valueOf(customerId));
            }
        });
    }

    /**
     * 修改跟进记录
     *
     * @param followTime
     * @param followId
     * @param content
     * @param customerId
     * @return
     */
    public static Task<Follow> updateFollows(long followTime, long followId, String content, final long customerId) {
        final Follow follow = new Follow();
        follow.content = content;
        follow.time = followTime;
        follow.id = followId;
        return Task.callInBackground(new Callable<Follow>() {
            @Override
            public Follow call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).putFollow(String.valueOf(customerId), follow);
            }
        });

    }

    /**
     * 删除跟进记录附件
     *
     * @param followId
     * @param ids
     * @return
     */
    public static Task<Boolean> deleteFollowRecordPhotos(final String followId, final String... ids) {
        return Task.callInBackground(new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).delFollowAttachments(followId, ids);
            }
        });
    }

    /**
     * 删除跟进记录
     *
     * @param customerId
     * @param followId
     * @return
     */
    public static Task<Boolean> deleteFollowRecord(final String customerId, final String followId) {
        return Task.callInBackground(new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).delFollow(customerId, followId);
            }
        });
    }

    /**
     * 删除客户信息附件
     *
     * @param customerId
     * @param attachmentId
     * @return
     */
    public static Task<Boolean> deleteCustomPhotos(final long customerId, final String attachmentId) {

        return Task.callInBackground(new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).delCustomerAttachment(String.valueOf(customerId), attachmentId);
            }
        });
    }

    /**
     * 获取标签
     *
     * @return
     */
    public static Task<List<CustomTagData>> getCustomTag() {
        return Task.callInBackground(new Callable<List<CustomTagData>>() {
            @Override
            public List<CustomTagData> call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).getCustomerTag();
            }
        });
    }

    /**
     * 客户线索列表
     * 勿删保留谢谢 by Roy 2017/2/8
     *
     * @return
     */
    public static Task<ClueDTO> getClues(final int page, final String source) {
        return Task.callInBackground(new Callable<ClueDTO>() {
            @Override
            public ClueDTO call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).getClues(page, source);
            }
        });
    }

    /**
     * 客户线索统计
     *
     * @return
     */
    public static Task<ClueStatistic> getClueStatic() {
        return Task.callInBackground(new Callable<ClueStatistic>() {
            @Override
            public ClueStatistic call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).getClueStatic();
            }
        });
    }

    /**
     * 获取最近跟进客户列表
     *
     * @param page
     * @return
     */
    public static Task<List<CustomLastedFollow>> getLastFollowList(final int page) {
        return Task.callInBackground(new Callable<List<CustomLastedFollow>>() {
            @Override
            public List<CustomLastedFollow> call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).getLastedFollowList(page);
            }
        });
    }

    /**
     * 获取计划书列表
     *
     * @param customId
     * @param page
     * @return
     */
    public static Task<List<Proposal>> getProposals(final long customId, final int page) {
        return Task.callInBackground(new Callable<List<Proposal>>() {
            @Override
            public List<Proposal> call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).getProposalList(String.valueOf(customId), page);
            }
        });
    }

    /**
     * 客户线索-关联客户
     *
     * @param clueId
     * @param customId
     * @return
     */
    public static Task<Custom> transferCustom(final String clueId, final long customId) {
        return Task.callInBackground(new Callable<Custom>() {
            @Override
            public Custom call() throws Exception {
                return ServiceManager.getService(CustomerStore.class).transferCustom(clueId, String.valueOf(customId));
            }
        });
    }
}